//
//  WmcHomeVC+AspectRatio.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/3/2.
//

import UIKit
import YPGraphicEngine

// MARK: - 用于设置画幅比例的扩展

extension YPGESunglasses.AspectRatio {
    /// 画幅高度
    var aspectHeight: CGFloat {
        return ratioHW * Wmc_screenWidth
    }

    struct Info {
        var title: String = "拍照画幅"
        var image: UIImage
        var color: UIColor = .white
        var name: String
    }

    /// 信息
    var info: Info {
        switch self {
        case .ar_1_1: return .init(image: "icon_aspect_1_1".wmc_image, name: "1:1")
        case .ar_4_3: return .init(image: "icon_aspect_3_4".wmc_image, name: "3:4")
        case .ar_16_9: return .init(image: "icon_aspect_9_16".wmc_image, name: "9:16")
        case .ar_full_screen: return .init(image: "icon_aspect_full_screen".wmc_image, name: "全屏")
        }
    }

    /// 相机画幅占屏幕的比率等级
    public enum RatioLevel {
        case low
        case lowHeight
        case medium
        case mediumHeight
        case height
    }

    /// 画幅占屏幕的比率
    public var ratioLevel: RatioLevel {
        let remainingHeight = aspectHeight / Wmc_screenHeight
        let low = 0.620
        let lowHeight = 0.750
        let medium = 0.822
        let mediumHeight = 0.930
        /**
         16:9:
          0.5622188905547226 都不透明
          0.7496233133433283 顶部透明，底部不透明
          0.9994958770614694 所有透明
         19.5:9:
          0.46205357142857145 都不透明
          0.6160698883928571 都不透明
          0.8214249776785715 顶部不透明，底部透明
          */
        if remainingHeight <= low {
            return .low // 都不透明 <= 0.620
        } else if remainingHeight > low, remainingHeight <= lowHeight {
            return .lowHeight // 顶部透明，底部不透明 0.620 ~ 0.750
        } else if remainingHeight > lowHeight, remainingHeight <= medium {
            return .medium // 顶部不透明，底部透明 0.750 ~ 0.822
        } else if remainingHeight > medium, remainingHeight <= mediumHeight {
            return .mediumHeight // 顶部底部都透明 0.822 ~ 0.930
        } else { // > 0.930
            return .height // 全透明
        }
    }
}

extension WmcHomeVC {
    /// 设置画幅比例
    func setAspectRatio(_ ratio: YPGESunglasses.AspectRatio = .ar_4_3, animated: Bool = true) {
        viewModel.currentAspectRatio = ratio
        cameraView?.setAspectRatio(ratio)
        // 计算后的画幅高度, 承接优化计算性能
        let aspectHeight = ratio.aspectHeight
        let ratioLevel = ratio.ratioLevel
        // 布局动画
        if animated {
            // 准备开始布局
            view.setNeedsUpdateConstraints()
            UIView.animate(withDuration: 0.3, // 用于同步相机预览视图的隐式动画时间和曲线
                           delay: 0,
                           options: .curveEaseInOut) { [weak self] in
                guard let weakSelf = self else { return }
                weakSelf.setLayoutStyle(aspectHeight: aspectHeight, ratioLevel: ratioLevel)
                // 强制布局
                weakSelf.view.layoutIfNeeded()
            }
        } else {
            setLayoutStyle(aspectHeight: aspectHeight, ratioLevel: ratioLevel)
            view.layoutIfNeeded()
        }
        
        // 回调拍照的画幅比
        homeDelegate?.cameraRatioLevelDidChange(.current, ratioLevel: ratioLevel)
    }

    func setLayoutStyle(aspectHeight: CGFloat, ratioLevel: YPGESunglasses.AspectRatio.RatioLevel) {
        // 2、设置布局
        setViewLayout(ratioLevel, aspectHeight: aspectHeight)
        // 3、设置样式
        setViewStyle(ratioLevel)
        // 4、更新水印位置
        watermarkContentView.yp.set(height: aspectHeight)
        setWatermarkPosit()
    }

    /// 设置布局
    func setViewLayout(_ ratioLevel: YPGESunglasses.AspectRatio.RatioLevel, aspectHeight: CGFloat) {
        // 底部模式选择视图不变
        let contentHeight = headerView.frame.size.height + Wmc_safeAreaBottom + YPGESunglasses.AspectRatio.ar_16_9.aspectHeight
        var functionSelectViewHeight = Wmc_screenHeight - contentHeight
        functionSelectViewHeight = max(functionSelectViewHeight, 42.wmc_autoVer)
        functionSelectView.snp.remakeConstraints { make in
            make.bottom.equalTo(-Wmc_safeAreaBottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(functionSelectViewHeight)
        }
        switch ratioLevel {
        case .low: // 都不透明
            // 相机布局
            cameraBackgroundView.snp.remakeConstraints { make in
                make.top.equalTo(headerView.snp.bottom)
                make.left.right.equalToSuperview()
                make.height.equalTo(aspectHeight)
            }
            // 底部工具栏视图
            footerView.snp.remakeConstraints { make in
                make.left.right.equalToSuperview()
                make.bottom.equalTo(functionSelectView.snp.top)
                make.top.equalTo(cameraBackgroundView.snp.bottom)
            }
        case .lowHeight: // 顶部透明，底部不透明
            // 相机布局
            cameraBackgroundView.snp.remakeConstraints { make in
                make.top.left.right.equalToSuperview()
                make.height.equalTo(aspectHeight)
            }
            // 底部工具栏视图
            footerView.snp.remakeConstraints { make in
                make.left.right.equalToSuperview()
                make.bottom.equalTo(functionSelectView.snp.top)
                make.top.equalTo(cameraBackgroundView.snp.bottom)
            }
        case .medium: // 顶部不透明，底部透明
            // 相机布局
            cameraBackgroundView.snp.remakeConstraints { make in
                make.top.equalTo(headerView.snp.bottom)
                make.left.right.equalToSuperview()
                make.height.equalTo(aspectHeight)
            }
            // 底部工具栏视图
            footerView.snp.remakeConstraints { make in
                make.left.right.equalToSuperview()
                make.bottom.equalTo(functionSelectView.snp.top)
                make.height.equalTo(168.wmc_autoVer)
            }
        case .mediumHeight,
             .height: // 顶部底部全透明
            // 相机布局
            cameraBackgroundView.snp.remakeConstraints { make in
                make.top.left.right.equalToSuperview()
                make.height.equalTo(aspectHeight)
            }
            // 底部工具栏视图
            footerView.snp.remakeConstraints { make in
                make.left.right.equalToSuperview()
                make.bottom.equalTo(functionSelectView.snp.top)
                make.height.equalTo(168.wmc_autoVer)
            }
        }
    }

    func setViewStyle(_ ratioLevel: YPGESunglasses.AspectRatio.RatioLevel) {
        switch ratioLevel {
        case .low: // 都不透明
            headerView.backgroundColor = .white
            headerView.isTransparent = false
            headerView.reloadItems()

            footerView.backgroundColor = .white
            footerView.isTransparent = false
            footerView.reloadItems()

            functionSelectView.backgroundColor = .white
            functionSelectView.isTransparent = false
            functionSelectView.reloadItems()

        case .lowHeight: // 顶部透明，底部不透明
            headerView.backgroundColor = .black.yp.alpha(0.3)
            headerView.isTransparent = true
            headerView.reloadItems()

            footerView.backgroundColor = .white
            footerView.isTransparent = false
            footerView.reloadItems()

            functionSelectView.backgroundColor = .white
            functionSelectView.isTransparent = false
            functionSelectView.reloadItems()

        case .medium: // 顶部不透明，底部透明
            headerView.backgroundColor = .white
            headerView.isTransparent = false
            headerView.reloadItems()
            footerView.backgroundColor = .black.yp.alpha(0.3)
            footerView.isTransparent = true
            footerView.reloadItems()

            functionSelectView.backgroundColor = .white
            functionSelectView.isTransparent = false
            functionSelectView.reloadItems()

        case .mediumHeight: // 顶部底部全透明
            headerView.backgroundColor = .black.yp.alpha(0.3)
            headerView.isTransparent = true
            headerView.reloadItems()

            footerView.backgroundColor = .black.yp.alpha(0.3)
            footerView.isTransparent = true
            footerView.reloadItems()

            functionSelectView.backgroundColor = .white
            functionSelectView.isTransparent = false
            functionSelectView.reloadItems()

        case .height: // 顶部底部全透明，模式选择也透明
            headerView.backgroundColor = .black.yp.alpha(0.3)
            headerView.isTransparent = true
            headerView.reloadItems()

            footerView.backgroundColor = .black.yp.alpha(0.3)
            footerView.isTransparent = true
            footerView.reloadItems()

            functionSelectView.backgroundColor = .black.yp.alpha(0.6)
            functionSelectView.isTransparent = true
            functionSelectView.reloadItems()
        }
    }

    /// 水印跟随画幅设置位置
    func setWatermarkPosit() {
        // 水印适配
        var pan: UIPanGestureRecognizer?
        watermarkContentView.watermarkView?.gestureRecognizers?.forEach { ges in
            if let _pan = ges as? UIPanGestureRecognizer {
                pan = _pan
            }
        }
        if let pan = pan {
            watermarkContentView.moveWatermarkView(pan)
        }
    }
}
